<?php

namespace Biz\model\package;

use Doraemon\pockets\datebase\BaseDbModel;

class OrderLogisticsLog extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db';
    const _CONFIG_SELECT_ = 'db_activity';
    const _DATABASE_ = 'hhz_activity';
    const _TABLE_NAME = 'order_logistics_log';

    const SHIPPED = 'shipped';//填写完物流单号

    const PENDING = 'pending';//查询中：新增包裹正在查询中，请等待
    const NOTFOUND = 'notfound';//查询不到：包裹信息目前查询不到
    const TRANSIT = 'transit';//查询不到：包裹信息目前查询不到
    const PICKUP = 'pickup';//到达待取：包裹正在派送中，或到达当地收发点
    const DELIVERED = 'delivered';//成功签收：包裹已被成功投递
    const EXPIRED = 'expired';//运输过久：包裹在很长时间内都未投递成功。快递包裹超过30天、邮政包裹超过60天未投递成功，该查询会被识别为此状态
    const UNDELIVERED = 'undelivered';//运输过久：包裹在很长时间内都未投递成功。快递包裹超过30天、邮政包裹超过60天未投递成功，该查询会被识别为此状态
    const EXCEPTION = 'exception';//可能异常：包裹退回、包裹丢失、清关失败等异常情况
    const INFORECEIVED = 'InfoReceived';//待上网：包裹正在等待被揽件


    //创建新的物流信息
    public static function save($data)
    {
        $data['addtime'] = time();
        return self::insert($data)->rowCount();
    }

    public static function editByOrderLogisticsNo($orderNo, $data)
    {
        $data['edittime'] = time();
        return self::update($data, ['order_logistics_no' => $orderNo])->rowCount();
    }

    public static function editByOrderId($id, $data)
    {
        $data['edittime'] = time();
        return self::update($data, ['id' => $id])->rowCount();
    }

    //根据物流单号获取详情
    public static function getInfoByOrderLogisticsNo($orderLogisticsNo)
    {
        return self::get('*', ['order_logistics_no' => $orderLogisticsNo]);
    }

    public static function getInfo($where)
    {
        $data = self::get(['id', 'logistics_info', 'carrier_code', 'order_no', 'order_logistics_no', 'status'], $where);
        if (!empty($data)) {
            $data['logistics_info'] = json_decode($data['logistics_info'], true);
        }
        return $data;
    }

    public static function selectInfo($where)
    {
        $data = self::select(['id', 'logistics_info', 'carrier_code', 'order_no', 'order_logistics_no', 'status'], $where);
        $result = [];
        foreach ($data as $key => $value) {
            if (!empty($value)) {
                $value['logistics_info'] = json_decode($value['logistics_info'], true);
            }
            $result[$value['order_logistics_no']] = $value;
        }

        return $result;
    }
}